﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Research.BLL;
using Research.Model;

namespace Research.Web
{
    public partial class RprjScheduleHandle : System.Web.UI.Page
    {
        readonly RprjMainBll _bllRprjMain = new RprjMainBll();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["Type"] == "Update")
                {
                    var mRprjSchedule =
                        _bllRprjMain.GetmRprjPlanScheduleByRprjIDAutoNum(Convert.ToInt32(Request["RprjID"]),
                                                                         Convert.ToInt32(Request["AutoNum"]));
                    BindPlanSchedule(mRprjSchedule);
                }
            }
        }

        private void BindPlanSchedule(MRprjPlanSchedule mRprjSchedule)
        {
            nbxSort.Text = mRprjSchedule.ScheduleSort.ToString();
            txtScheduleName.Text = mRprjSchedule.ScheduleName;
            dpPlanBegDate.SelectedDate = Convert.ToDateTime(mRprjSchedule.PlanBegDate);
            dpPlanEndDate.SelectedDate = Convert.ToDateTime(mRprjSchedule.PlanEndDate);
            txtMemo.Text = mRprjSchedule.PlanMemo;
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Request["Type"] == "Add")
            {
                var mRprjSchedule = new MRprjPlanSchedule
                                                      {
                                                          RprjID = Convert.ToInt32(Request["RprjID"]),
                                                          ScheduleSort = Convert.ToInt32(nbxSort.Text),
                                                          ScheduleName = txtScheduleName.Text,
                                                          PlanBegDate = dpPlanBegDate.SelectedDate,
                                                          PlanEndDate = dpPlanEndDate.SelectedDate,
                                                          PlanMemo = txtMemo.Text
                                                      };
                _bllRprjMain.InsertRprjPlanSchedule(mRprjSchedule);
            }
            else if (Request["Type"] == "Update")
            {
                var mRprjSchedule = new MRprjPlanSchedule
                                        {
                                            RprjID = Convert.ToInt32(Request["RprjID"]),
                                            AutoNum = Convert.ToInt32(Request["AutoNum"]),
                                            ScheduleSort =  Convert.ToInt32(nbxSort.Text),
                                            PlanBegDate = dpPlanBegDate.SelectedDate,
                                            PlanEndDate = dpPlanEndDate.SelectedDate,
                                            PlanMemo = txtMemo.Text,
                                            ScheduleName = txtScheduleName.Text
                                        };
                    
                    

                _bllRprjMain.UpdateRprjPlanSchedule(mRprjSchedule);
            }
            ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHideRefreshReference());
        }
    }
}